﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emoney.UserTraining.Model;
using NHibernate.Linq;

namespace Emoney.UserTraining.DAL
{
    public class Traning_MeetingTypeDAL
    {
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public List<Traning_MeetingType> GetAllList()
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var data = context.Traning_MeetingType;
                data = data.Where(p => p.isDelete == "0").Cacheable();
                return data.ToList();
            }
        }


        /// <summary>
        /// 新增和修改
        /// </summary>
        /// <param name="model"></param>
        public void AddUpdate(Traning_MeetingType model)
        {
            int modelid = model.id;
            using (TemporaryContext context = new TemporaryContext())
            {
                var data = context.Traning_MeetingType;
                var c = data.FirstOrDefault(p => p.isDelete == "0" && p.meetingType == model.meetingType);
                if (c != null)
                {
                    model.meetingType = c.meetingType;
                }
                if (!string.IsNullOrEmpty(modelid.ToString()))//修改会议类型（历史数据已存类型名称一并修改）
                {
                    var c1 = data.FirstOrDefault(x => x.id == modelid);
                    if (c1 != null)
                    {
                        UpdateMeetingtypeForNet(c1.meetingType, model.meetingType);
                    }
                }
            }
            using (TemporaryContext context1 = new TemporaryContext())
            {
                context1.Session.SaveOrUpdate(model);
                context1.Session.Flush();
            }
        }
        /// <summary>
        /// 修改会议类型（已添加的网络会议)
        /// </summary>
        /// <param name="oldtype"></param>
        /// <param name="newtype"></param>
        public void UpdateMeetingtypeForNet(string oldtype,string newtype)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                var list = context.Traning_NetworkMeeting.Where(x => x.meeting_type == oldtype).ToList();
                foreach (Traning_NetworkMeeting item in list)
                {
                    item.meeting_type = newtype;
                    context.Session.Update(item);
                    context.Session.Flush();
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            using (TemporaryContext context = new TemporaryContext())
            {
                Traning_MeetingType c = new Traning_MeetingType() { id = id };
                context.Session.Delete(c);
                context.Session.Flush();
                return 1;
            }
        }

    }
}
